/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.components;

import com.nokia.example.wordpress.helpers.TextWrapper;
import com.nokia.example.wordpress.networking.Comment;
import com.nokia.example.wordpress.networking.GetGravatarOperation;
import com.nokia.example.wordpress.views.ViewMaster;
import com.nokia.example.wordpress.views.Visual;
import java.io.ByteArrayInputStream;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * A drawer class for rendering the rows in the comments list Visual.
 */
public class CommentDrawer implements ListDrawer {

    private final int ROW_HEIGHT = Math.max(3 * Visual.SMALL_FONT.getHeight() + 2, 64 + 2); // 64 is the gravatar size, +2 small margin
    private Hashtable gravatarCache;
    private Image defaultGravatarIcon;
    private ViewMaster view;

    private class Icon {

        Icon(Image defaultIcon) {
            this.image = defaultIcon;
        }
        Image image = null;
    }

    public CommentDrawer(ViewMaster view, Hashtable gravatarCache, Image defaultGravatarIcon) {
        this.view = view;
        this.gravatarCache = gravatarCache;
        this.defaultGravatarIcon = defaultGravatarIcon;
    }

    public int itemHeight() {
        return ROW_HEIGHT;
    }

    /**
     * Returns a gravar icon for a comment. Loads from network if not available,
     * after loading the main view is refreshed.
     * @param comment
     * @return
     */
    public Image getIcon(Comment comment) {

        final String email = comment.author_email.trim();
        if (email.equals("")) {
            return null;
        }

        Icon icon = (Icon) gravatarCache.get(email);
        if (icon == null) {
            icon = new Icon(defaultGravatarIcon);
            gravatarCache.put(email, icon);
            System.out.println("getting icon for " + email);
            GetGravatarOperation.Listener listener = new GetGravatarOperation.Listener() {

                public void gravatarReceived(String email, byte[] data) {
                    try {
                        System.out.println("got icon for " + email);
                        Icon icon = (Icon) gravatarCache.get(email);
                        icon.image = Image.createImage(new ByteArrayInputStream(data));
                        view.draw();
                    } catch (java.io.IOException ex) {
                        System.out.print(ex.getMessage());
                    }
                }
            };
            GetGravatarOperation op = new GetGravatarOperation(listener, comment.author_email);
            op.start();
        }
        return icon.image;
    }

    public void drawItem(Vector data, Graphics g, int itemIndex, int x, int y, int width, int height, boolean focused) {

        view.drawBackground(g, x, y, width, ROW_HEIGHT, focused);

        Comment comment = (Comment) data.elementAt(itemIndex);

        Image icon = getIcon(comment);
        if (icon != null) {
            g.drawImage(icon, 0, y, g.TOP | g.LEFT);
            x += icon.getWidth() + 2; // margins
            width -= icon.getWidth() + 2; // margins
        }
        int commentX = x;

        g.setColor(Visual.LIST_SECONDARY_COLOR);
        g.setFont(Visual.SMALL_FONT);

        int linesLeft = 3;
        String firstLine = comment.author + " on ";

        if(comment.post != null) {
            firstLine += comment.post.title;
        } else {
            //Post not loaded yet. A fixed number of posts were loaded earlier, but not enough it seems.
            firstLine += " ";
        }

        Vector lines = TextWrapper.wrapTextToWidth(firstLine, width, Visual.SMALL_FONT);
        g.drawString((String) lines.elementAt(0), commentX, y, g.TOP | g.LEFT);
        y += Visual.SMALL_FONT.getHeight();
        linesLeft--;
        if (lines.size() > 1) {
            g.drawString((String) lines.elementAt(1), commentX, y, g.TOP | g.LEFT);
            y += Visual.SMALL_FONT.getHeight();
            linesLeft--;
        }

        g.setColor(Visual.LIST_PRIMARY_COLOR);
        lines = TextWrapper.wrapTextToWidth(comment.content, width, Visual.SMALL_FONT);
        int linesToDraw = Math.min(lines.size(), linesLeft);
        for (int i = 0; i < linesToDraw; i++) {
            g.drawString((String) lines.elementAt(i), commentX, y, g.TOP | g.LEFT);
            y += Visual.SMALL_FONT.getHeight();
        }
    }
}
